<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('after.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">


            <template #order_detail>
                <el-table-column label="原订单号" width="200" align="center">
                    <template #default="scope">
                        <el-link type="success" @click="orderInfo(scope.row.order_id)"> {{scope.row.order.order_no}}</el-link>
                    </template>
                </el-table-column>
            </template>

        </Table>

        <!-- 表单 -->
        <PopupForm />
        <After/>
        <order_info/>
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import createAxios from "/@/utils/axios";
import {ElMessage} from "element-plus";
import {auth} from "/@/utils/common";
import After from './after.vue'
import order_info from '../order/info.vue'

defineOptions({
    name: 'after',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

const after_auth = auth('after')
const refund_auth = auth('refund')
optButtons.push(
    {
        render: 'tipButton',
        // name 是任意的
        name: 'after',
        // title 是语言翻译 key
        title: '售后',
        text: '',
        type: 'warning',
        // icon: 'fa fa-search-plus icon',
        icon: 'el-icon-Failed',
        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/after/after?id='+row.id,
                method: 'get',
                data: {
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {

                        // 在 extend 上自定义一个变量标记详情弹窗显示状态，详情组件内以此判断显示即可！

                        baTable.table.extend!.showAfter = true
                        baTable.table.extend!.infoDataAfter = res.data.row
                    } else {

                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }


                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            return after_auth;
        },
    },

    {
        render: 'confirmButton',
        name: 'refund_money',
        title: '退款',
        // text: '退款',
        type: 'info',
        icon: 'fa fa-sign-out',
        class: 'table-row-info',
        popconfirm: {
            title:'确认退款吗？',
        },

        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/after/refund',
                method: 'post',
                data: {
                    id:row.id,
                }
            }, {
                loading: true
            }, {
                text: '退款中...'
            })
                .then((res) => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })

                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            return refund_auth && row.refund_status == 3;
        },
    },
)
/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/After/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('after.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            { label: t('after.user__nickname'), width:100, prop: 'user.nickname', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('after.refund_no'), width:200, prop: 'refund_no', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('after.order_id'), prop: 'order_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('after.order__order_no'),show:false, width:200, prop: 'order.order_no', align: 'center', operatorPlaceholder: t('Fuzzy query'),  operator: 'LIKE' },
            { render: 'slot', slotName: 'order_detail', operator: false },
            // { label: t('after.user_id'), prop: 'user_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },

            // { label: t('after.start_type'), prop: 'start_type', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '1': t('after.start_type 1'), '2': t('after.start_type 2'), '3': t('after.start_type 3') } },

            // { label: t('after.order_goods_ids'), prop: 'order_goods_ids', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            // { label: t('after.ordergoods__name'), prop: 'orderGoods.name', align: 'center', render: 'tags', operator: false },
            // { label: t('after.ordergoods__name'), prop: 'order_goods_ids', align: 'center', operator: 'FIND_IN_SET', show: false, comSearchRender: 'remoteSelect', remote: { pk: 'xxz_ordergoods.id', field: 'name', remoteUrl: '/admin/Ordergoods/index', multiple: true } },
            // { label: t('after.order_status'), prop: 'order_status', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.money'), width:100, prop: 'money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.freight_money'), width:100, prop: 'freight_money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.actual_money'), width:100, prop: 'actual_money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.cash_money'), width:100, prop: 'cash_money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.balance'), width:100, prop: 'balance', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('after.return_type'), width:100, prop: 'return_type', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '1': t('after.return_type 1'), '2': t('after.return_type 2'), '3': t('after.return_type 3'), '4': t('after.return_type 4') } },
            { label: t('after.reason'), width:200, prop: 'reason', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('after.supplement'), prop: 'supplement', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('after.images'), prop: 'images', align: 'center', render: 'images', operator: false },
            // { label: t('after.return_logistics_no'), prop: 'return_logistics_no', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('after.status'), width:100, prop: 'status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '1': t('after.status 1'), '20': t('after.status 20'), '30': t('after.status 30'), '50': t('after.status 50'), '60': t('after.status 60') } },
            // { label: t('after.handle_time'), prop: 'handle_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('after.refund_status'),width:100, prop: 'refund_status', render: 'tag', align: 'center', operator: 'eq', sortable: false, replaceValue: { '0': t('after.refund_status 0'), '1': t('after.refund_status 1'), '2': t('after.refund_status 2'), '3': t('after.refund_status 3'), '4': t('after.refund_status 4'), '9': t('after.refund_status 9') } },
            // { label: t('after.refund_msg'), prop: 'refund_msg', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('after.refund_time'), prop: 'refund_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('after.reject_reason'), width:150, prop: 'reject_reason', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('after.remark'), width:100, prop: 'remark', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('after.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('after.update_time'), prop: 'update_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false,
                fixed: 'right' as const },
        ],
        dblClickNotEditColumn: [] as string[],
    },
    {
        defaultItems: { user_id: null, start_type: '1', refund_no: null, order_goods_ids: null, order_status: 0, money: 0, actual_money: 0, return_type: '0', reason: null, supplement: null, images: null, return_logistics_no: null, status: '0', handle_time: null, refund_status: '0', refund_msg: null, reject_reason: null, remark: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

//  原订单信息
const orderInfo = (order_id: any) => {

    // 获取详情
    createAxios({
        url: '/admin/order/info?id='+order_id,
        method: 'get',
        data: {

        }
    }, {
        loading: true
    }, {
        text: '加载中...'
    })
        .then((res) => {
            if (res.code == 1) {

                // 在 extend 上自定义一个变量标记详情弹窗显示状态，详情组件内以此判断显示即可！

                baTable.table.extend!.showInfo = true
                baTable.table.extend!.infoData = res.data.row
            } else {

                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }


        })
        .catch((resx) => {
        })
}
</script>

<style scoped lang="scss">

</style>
